// pages/modificationAddress/modificationAddress.js
var utils = require('../../utils/util.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // region: ['重庆市', '重庆市', '沙坪坝区'],
    id:'',
    name:'',
    phone:'',
    region: [],
    address:'',
    isChecked:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    var id = options.id
    _this.setData({
      id:id
    })
    _this.getAddressInfo(id)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 获取地址信息
   */
  getAddressInfo:function (id) {
    var _this = this
    wx.request({
      url: app.globalData.requestip + "&c=Address&a=getAddressInfo",
      data: {
        id:id
      },
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          var data = res.data.data
          var region = [];
          region.push(data.province)
          region.push(data.city)
          region.push(data.area)
          _this.setData({
            name: data.name,
            phone: data.phone,
            address: data.address,
            region: region,
            isChecked: parseInt(data.default),
          })
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  /**
   * 选择地址
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },

  /**
   * 设置为默认按钮
   */
  checkChange:function () {
    var check = {}
    check['isChecked'] = this.data.isChecked ? false : true
    this.setData(check)
  },

  /**
   * 删除地址
   */
  removeAddr: function () {
    var _this = this
    var id = _this.data.id
    wx: wx.showModal({
      content: '是否删除？',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.requestip + "&c=Address&a=deleteAddress",
            data: {
              id: id
            },
            header: app.globalData.header,
            success: function (res) {
              if (res.data.code == 0) {
                wx.showToast({
                  title: '删除成功',
                  duration: 2000,
                  success: function (res) {
                    wx.navigateBack({
                      delta: 1,
                    })
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              } else {
                utils.loadFail(res)
              }
            },
            fail: function (res) {
              utils.loadRequestFail(res)
            }
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  /**
   * 保存按钮
   */
  tapSave: function (e) {
    var _this = this
    var province = e.detail.value.region[0]
    var city = e.detail.value.region[1]
    var area = e.detail.value.region[2]
    var id = _this.data.id
    var is_default = 0
    if (_this.data.isChecked) {
      is_default = 1
    }
    wx.request({
      url: app.globalData.requestip + "&c=Address&a=updateAddress",
      data: {
        id:id,
        name: e.detail.value.name,
        phone: e.detail.value.phone,
        province:province,
        city:city,
        area:area,
        address: e.detail.value.address,
        is_default:is_default
      },
      header: app.globalData.header,
      success: function (res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '保存成功',
            duration: 2000,
            success: function (res) {
              wx.navigateBack({
                delta: 1,
              })
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        } else {
          utils.loadFail(res)
        }
      },
      fail: function (res) {
        utils.loadRequestFail(res)
      }
    })
  },

  /**
   * 取消按钮
   */
  tapCancel:function () {
    wx.navigateBack({
      delta: 1,
    })
  }
})

